package com.yidu.BusinessParameters.service.impl;

import com.yidu.BusinessData.entity.SecurityEntity;
import com.yidu.BusinessParameters.entity.Trade;
import com.yidu.BusinessParameters.mapper.TradeMapper;
import com.yidu.BusinessParameters.service.TradeService;
import com.yidu.CashAdministration.entity.MoneyAllocateEntity;
import com.yidu.CashAdministration.mapper.MoneyAllocateMapper;
import com.yidu.Util.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 交易数据实现类
 * @author zhouhongyu
 * @since 2022/8/27
 * @version 1.0
 */
@Service
public class TradeServiceImpl implements TradeService {
    //自动装配
    @Autowired
    private TradeMapper tradeMapper;
    @Resource
    private MoneyAllocateMapper moneyAllocateMapper;
    @Resource
    private Dbutil dbutil;
    /**
     * 多条件查询
     * @param securityId
     * @param tradeDate
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Trade> selectTrade(String securityId,String tradeDate, int page, int limit) {
        Map map = new HashMap<>();
        map.put("strWreen",strWreen(securityId,tradeDate));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        return tradeMapper.selectTrade(map);
    }

    /**
     * 条数
     * @return
     */
    @Override
    public int selectCount() {
        return tradeMapper.selectCount();
    }

    /**
     * 重写增加交易数据的方法
     * @param trade
     * @return
     */
    @Override
    public int insertTrade(Trade trade) {
        return tradeMapper.insertTrade(trade);
    }

    /**
     * 重写修改交易数据的方法
     * @param trade
     * @return
     */
    @Override
    public int updateTrade(Trade trade) {
        return tradeMapper.updateTrade(trade);
    }

    /**
     * 重写删除交易数据的方法
     * @param tradeId
     * @return
     */
    @Override
    public int deleteTrade(String tradeId) {
        return tradeMapper.deleteTrade(tradeId);
    }


 /*   @Override
    public List<Trade> selectSettlement(String tradeType,String tradeDate,int page,int limit) {
        Map maps = new HashMap<>();
        maps.put("tradeType1",tradeType1(tradeType,tradeDate));
        maps.put("page",(page-1)*limit);
        maps.put("limit",limit);
        return tradeMapper.selectSettlement(maps);
    }*/
    /**
     * 重写交易结算查询已结算方法
     * @param
     * @return
     */
    @Override
    public List<Trade> selectSettlement(String tradeType, String tradeDate,int page,int limit) {
        Map map = new HashMap<>();
        map.put("tradeType1",tradeType1(tradeType,tradeDate));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        return tradeMapper.selectSettlement(map);
    }

    /**
     * 重写交易结算查询未结算方法
     * @param.
     * @return
     */
    @Override
    public List<Trade> selectSettlement2(String tradeType,String tradeDate,int page,int limit) {
        Map map = new HashMap<>();
        map.put("tradeType1",tradeType1(tradeType,tradeDate));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        return tradeMapper.selectSettlement2(map);
    }

    /**
     * 重写修改未结算方法
     * @param trade
     * @return
     */
    @Override
    public int updateSettlement(String trade) {
        /*Random r = new Random();
        String count = "";*/
        //将交易数据的json格式转成List集合用对象单独点值给资金调拨
        List<Trade> Trades = SysUtil.jsonToArrayList(trade,Trade.class);
        int i = 0;
        int i1=0;
        for (Trade e : Trades) {
            /*count = String.valueOf(r.nextInt());*/
          /*  System.out.println(count);*/
            //创建一个资金调拨的实体类
            MoneyAllocateEntity moneyAllocate = new MoneyAllocateEntity();
            String maxId3 = dbutil.getMaxId3(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime, e.getSettleDate());
            moneyAllocate.setMoneyAllocateId(maxId3);//资金调拨的Id
            moneyAllocate.setFundId(e.getFundId());//基金代码
            moneyAllocate.setCashId(e.getCashId());//现金账户Id
            moneyAllocate.setAllocateMoney(e.getSettleMoney()); //调拨金额
            moneyAllocate.setAllocateType(3); //调拨类型
            if(e.getTradeType()==2){
                moneyAllocate.setAllocateFlow(1); //调拨方向
            }else{
                moneyAllocate.setAllocateFlow(-1); //调拨方向
            }
            moneyAllocate.setBusinessId(e.getTradeId()); //业务Id
            moneyAllocate.setAllocateDate(e.getSettleDate()); //调拨日期
            moneyAllocate.setBusinessDate(e.getTradeDate()); //业务日期
            System.out.println("moneyAllocate：" + moneyAllocate);
            //将未结算改成已结算

            i1 = tradeMapper.updateSettlement(e.getTradeId());
            System.out.println("状态码="+e.getSettleState());
            //已结算就新增
            if (i1 == 1){
                System.out.println("已修改成已结算");
                System.out.println("moneyAllocate：" + moneyAllocate);
                //结算完之后进行资金调拨的添加
                i = moneyAllocateMapper.moneyAllocateInsert(moneyAllocate);
            }
            System.out.println("dealId：" + e.getTradeId());
        }
        return i;
    }

    /**
     * 已结算修改成未结算
     * @param tradeId
     * @return
     */
    @Override
    public int updateSettled(String tradeId) {
        return tradeMapper.updateSettled(tradeId);
    }

    /**
     * 根据业务编号删除资金调拨对应的数据
     * @param businessId
     * @return
     */
    @Override
    public int deleteMoneyAllocate(String businessId) {
        return tradeMapper.deleteMoneyAllocate(businessId);
    }


    /**
     * 交易数据多条件拼接查询
     * @param securityId 证券ID
     * @param tradeDate 交易日期
     * @return
     */
    public String strWreen(String securityId,String tradeDate){
        StringBuffer stringBuffer = new StringBuffer("");
        if(securityId!=null && !securityId.equals("")){
                stringBuffer.append(" and a.securityId like'%" +securityId+ "%'");
        }if (tradeDate!=null && !tradeDate.equals("")){
                stringBuffer.append("and a.tradeDate like'%" + tradeDate + "%'");
        }
        System.out.println(stringBuffer+"builber");
        return stringBuffer.toString();
    }

    /**
     * 拼接交易结算的多条件查询
     * @param tradeType
     * @param tradeDate
     * @return
     */
    public String tradeType1(String tradeType,String tradeDate){
        StringBuffer stringBuffer2 = new StringBuffer("");
        if (tradeType != null && !tradeType.equals("") && !tradeType.equals("0")){
            stringBuffer2.append(" and a.tradeType="+tradeType);
        }if (tradeDate != null && !tradeDate.equals("")) {
            stringBuffer2.append(" and a.tradeDate like'%" + tradeDate + "%'");
        }
        System.out.println(stringBuffer2+"builber");
        return stringBuffer2.toString();
    }
}
